<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="己方上单" prop="top">
        <el-select v-model="queryParams.top"
                   @keyup.enter.native="handleQuery" filterable>
          <el-option v-for="item in topList" :key="item.nickName"
            :label="item.nickName" :value="item.nickName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="己方打野" prop="jungle">
        <el-select v-model="queryParams.jungle" @keyup.enter.native="handleQuery" filterable>
          <el-option v-for="item in heroList" :key="item.nickName"
                     :label="item.nickName" :value="item.nickName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="己方中单" prop="mid">
        <el-select v-model="queryParams.mid" @keyup.enter.native="handleQuery" filterable>
          <el-option v-for="item in midList" :key="item.nickName"
                     :label="item.nickName" :value="item.nickName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="己方ad" prop="ad">
        <el-select v-model="queryParams.ad" @keyup.enter.native="handleQuery" filterable>
          <el-option v-for="item in adList" :key="item.nickName"
                     :label="item.nickName" :value="item.nickName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="己方辅助" prop="sup">
        <el-select v-model="queryParams.sup" @keyup.enter.native="handleQuery" filterable>
          <el-option v-for="item in supList" :key="item.nickName"
                     :label="item.nickName" :value="item.nickName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="对方上单" prop="top2">
        <el-select v-model="queryParams.top2" @keyup.enter.native="handleQuery" filterable>
          <el-option v-for="item in top2List" :key="item.nickName"
                     :label="item.nickName" :value="item.nickName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="对方打野" prop="jungle2">
        <el-select v-model="queryParams.jungle2" @keyup.enter.native="handleQuery" filterable>
          <el-option v-for="item in jun2List" :key="item.nickName"
                     :label="item.nickName" :value="item.nickName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="对方中单" prop="mid2">
        <el-select v-model="queryParams.mid2" @keyup.enter.native="handleQuery" filterable>
          <el-option v-for="item in mid2List" :key="item.nickName"
                     :label="item.nickName" :value="item.nickName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="对方ad" prop="ad2">
        <el-select v-model="queryParams.ad2" @keyup.enter.native="handleQuery" filterable>
          <el-option v-for="item in ad2List" :key="item.nickName"
                     :label="item.nickName" :value="item.nickName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="对方辅助" prop="sup2">
        <el-select v-model="queryParams.sup2" @keyup.enter.native="handleQuery" filterable>
          <el-option v-for="item in sup2List" :key="item.nickName"
                     :label="item.nickName" :value="item.nickName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="红/蓝方" prop="ifWin">
        <el-input
          v-model="queryParams.ifWin"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="游戏时间" prop="gameTime">
        <el-date-picker clearable
          v-model="queryParams.gameTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="我的位置" prop="myPosition">
        <el-input
          v-model="queryParams.myPosition"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="战犯" prop="criminal" v-show="false">
        <el-input
          v-model="queryParams.criminal"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="红蓝方" prop="blueRed">
        <el-input
          v-model="queryParams.blueRed"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['lol:record:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['lol:record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['lol:record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['lol:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="false"/>
      <el-table-column label="己方上单" align="center" prop="top" />
      <el-table-column label="己方打野" align="center" prop="jungle" />
      <el-table-column label="己方中单" align="center" prop="mid" />
      <el-table-column label="己方ad" align="center" prop="ad" />
      <el-table-column label="己方辅助" align="center" prop="sup" />
      <el-table-column label="对方上单" align="center" prop="top2" />
      <el-table-column label="对方打野" align="center" prop="jungle2" />
      <el-table-column label="对方中单" align="center" prop="mid2" />
      <el-table-column label="对方ad" align="center" prop="ad2" />
      <el-table-column label="对方辅助" align="center" prop="sup2" />
      <el-table-column label="输/赢" align="center" prop="ifWin" />
      <el-table-column label="时间" align="center" prop="gameTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gameTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="我的位置" align="center" prop="myPosition" />
      <el-table-column label="战犯" align="center" prop="criminal" />
      <el-table-column label="红蓝方" align="center" prop="blueRed" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['lol:record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['lol:record:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改游戏记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col style="width: 20%">
            <el-form-item label="己方上单" prop="top">
              <el-select v-model="form.top" @keyup.enter.native="handleQuery" filterable allow-create>
                <el-option v-for="item in topList" :key="item.nickName"
                           :label="item.nickName" :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col style="width: 20%">
            <el-form-item label="己方打野" prop="jungle">
              <el-select v-model="form.jungle" @keyup.enter.native="handleQuery" filterable allow-create>
                <el-option v-for="item in heroList" :key="item.nickName"
                           :label="item.nickName" :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col style="width: 20%">
            <el-form-item label="己方中单" prop="mid">
              <el-select v-model="form.mid" @keyup.enter.native="handleQuery" filterable allow-create>
                <el-option v-for="item in midList" :key="item.nickName"
                           :label="item.nickName" :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col style="width: 20%">
            <el-form-item label="己方ad" prop="ad">
              <el-select v-model="form.ad" @keyup.enter.native="handleQuery" filterable allow-create>
                <el-option v-for="item in adList" :key="item.nickName"
                           :label="item.nickName" :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col style="width: 20%">
            <el-form-item label="己方辅助" prop="sup">
              <el-select v-model="form.sup" @keyup.enter.native="handleQuery" filterable allow-create>
                <el-option v-for="item in supList" :key="item.nickName"
                           :label="item.nickName" :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col style="width: 20%">
            <el-form-item label="对方上单" prop="top2">
              <el-select v-model="form.top2" @keyup.enter.native="handleQuery" filterable allow-create>
                <el-option v-for="item in top2List" :key="item.nickName"
                           :label="item.nickName" :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col style="width: 20%">
            <el-form-item label="对方打野" prop="jungle2">
              <el-select v-model="form.jungle2" @keyup.enter.native="handleQuery" filterable allow-create>
                <el-option v-for="item in jun2List" :key="item.nickName"
                           :label="item.nickName" :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col style="width: 20%">
            <el-form-item label="对方中单" prop="mid2">
              <el-select v-model="form.mid2" @keyup.enter.native="handleQuery" filterable allow-create>
                <el-option v-for="item in mid2List" :key="item.nickName"
                           :label="item.nickName" :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col  style="width: 20%">
            <el-form-item label="对方ad" prop="ad2">
              <el-select v-model="form.ad2" @keyup.enter.native="handleQuery" filterable allow-create>
                <el-option v-for="item in ad2List" :key="item.nickName"
                           :label="item.nickName" :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col style="width: 20%">
            <el-form-item label="对方辅助" prop="sup2">
              <el-select v-model="form.sup2" @keyup.enter.native="handleQuery" filterable allow-create>
                <el-option v-for="item in sup2List" :key="item.nickName"
                           :label="item.nickName" :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col style="width: 20%">
            <el-form-item label="胜/负" prop="ifWin">
<!--              <el-input v-model="form.ifWin" placeholder="胜/负" />-->
              <el-select v-model="form.ifWin" @keyup.enter.native="handleQuery" filterable allow-create>
                <el-option v-for="item in ifWinList" :key="item.nickName"
                           :label="item.nickName" :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col style="width: 20%">
            <el-form-item label="游戏时间" prop="gameTime">
              <el-date-picker clearable  style="width: 150px"
                              v-model="form.gameTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col style="width: 20%">
            <el-form-item label="我的位置" prop="myPosition">
              <el-input v-model="form.myPosition" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col style="width: 20%">
            <el-form-item label="战犯" prop="criminal">
              <el-input v-model="form.criminal" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col style="width: 20%">
            <el-form-item label="红蓝方" prop="blueRed">
              <el-input v-model="form.blueRed" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/lol/record";

import { listAll,noPageList } from "@/api/lol/hero";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      heroList: [],
      topList: [],
      top2List: [],
      midList: [],
      mid2List: [],
      adList: [],
      ad2List: [],
      supList: [],
      sup2List: [],
      jun2List: [],
      // 游戏记录表格数据
      recordList: [],
      ifWinList: [{value:'win',label:'win'},{value: 'win',label:'lose'}],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        top: null,
        jungle: null,
        mid: null,
        ad: null,
        sup: null,
        top2: null,
        jungle2: null,
        mid2: null,
        ad2: null,
        sup2: null,
        ifWin: null,
        gameTime: null,
        myPosition: null,
        criminal: null,
        blueRed: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getHeroList();
  },
  methods: {
    /** 查询游戏记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getHeroList(){
      listAll().then(response => {
        this.heroList = response.data;
      });
      noPageList().then(response=>{
        this.topList=response.data.topList;
        this.top2List=response.data.top2List;
        this.midList=response.data.midList;
        this.mid2List=response.data.mid2List;
        this.adList=response.data.adList;
        this.ad2List=response.data.ad2List;
        this.supList=response.data.supList;
        this.sup2List=response.data.sup2List;
        this.jun2List=response.data.jun2List;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        top: null,
        jungle: null,
        mid: null,
        ad: null,
        sup: null,
        top2: null,
        jungle2: null,
        mid2: null,
        ad2: null,
        sup2: null,
        ifWin: null,
        gameTime: null,
        myPosition: null,
        criminal: null,
        blueRed: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加游戏记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改游戏记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除游戏记录编号为"' + ids + '"的数据项？').then(function() {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lol/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
